package 集合;

import org.junit.Test;

import java.util.*;

/**
 * Map  1.key:无序的、不可重复的,使用Set存储所有key,须重写key所在类的equals()和hanshCode()方法
 *      2.value:无序的、可重复的,使用Collection存储所有value
 *      3.entry(key-value):无序的、不可重复的,使用Set存储所有entry
 *
 */
public class mapDemo {
    @Test
    public void test()  {
        HashMap<String, Double> hashMap = new HashMap();
        HashMap<String, Double> map = new HashMap<>();
        //1.添加
        hashMap.put("jim",123.0);
        //2.修改
        hashMap.put("jim",hashMap.get("jim")+100);
        hashMap.put("jim1",hashMap.get("jim")+100);
        hashMap.put("jim2",hashMap.get("jim")+100);
        hashMap.put("jim3",hashMap.get("jim")+100);
        hashMap.put("jim4",hashMap.get("jim")+100);
        //3.添加其他HashMap
        hashMap.putAll(map);
        //4.移除，返回value值
        hashMap.remove("jim");
        //5.移除元素/全部清空
        hashMap.remove("jim");
        hashMap.clear();
        //6.查询，返回value值
        hashMap.get("jim");
        //7.是否为空，返回true、false
        hashMap.isEmpty();
        //8.是否包含key/value，返回true、false
        hashMap.containsKey("");
        hashMap.containsValue("");
        //9.对比相等
        hashMap.equals(map);
        //10.遍历所有key,keySet()
        Set<String> keySet = hashMap.keySet();
        Iterator<String> iterator = keySet.iterator();
        while (iterator.hasNext())
            System.out.println(iterator.next());
        //11.遍历所有value,values()
        Collection<Double> values = hashMap.values();
        Iterator<Double> iterator1 = values.iterator();
        while (iterator1.hasNext())
            System.out.println(iterator1.next());
        //11.遍历所有entry,entrySet()
        Set<Map.Entry<String, Double>> entrySet = hashMap.entrySet();
        Iterator<Map.Entry<String, Double>> iterator2 = entrySet.iterator();
        while (iterator2.hasNext()){
            Map.Entry<String, Double> entry=iterator2.next();
            String key=entry.getKey();
            Double value=entry.getValue();
            System.out.println(key+""+value);
            //System.out.println(iterator2.next());
        }

    }
    //TreeMap排序
    @Test
    public void test1(){
        TreeMap list = new TreeMap((o1, o2) -> {
            if (o1 instanceof Emp){
                Emp e1=(Emp)o1;
                Emp e2=(Emp)o2;
                return Integer.compare(e1.age,e2.age);
            }
            return 0;
        });
    }
    public void test2(){

    }
}
